programming4us
           
 
 
SQL Server

SQL Server Integration Services : Using bcp (part 2) - Fundamentals of Exporting and Importing Data

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
11/30/2010 3:11:17 PM

Fundamentals of Exporting and Importing Data

One of the great things about bcp is its ease of use. This section runs through a couple simple examples and provides full explanations. All tables used here can be found in the AdventureWorks2008 sample database supplied by Microsoft in SQL Server 2008.

Let’s start by exporting product data from AdventureWorks2008 that may be needed by a sales team for reference in Excel format (a .csv file). To do this, you simply export the Product table data into a comma-delimited file. You need to specify the following with bcp in this case:

  • The full table name (in this case, AdventureWorks2008.Production.Product)

  • The direction of bcp (OUT in this case because it is exporting data out)

  • Data filename to hold the exported data (in this case, products.dat)

  • The server name DBARCH-LT2\SQL08DE01 for this example (in this case, –S DBARCH-LT2\SQL08DE01)

  • The username SA (in this case, –U sa)

  • The password (in this case, –P xyz)

  • A comma as the column delimiter (in this case, –t ",")

  • That this should be exported in character data format (in this case, –c)

At the command prompt, you execute the following:

C:> BCP AdventureWorks2008.Production.Product OUT products.dat
–S DBARCH-LT2\SQL08DE01 –U sa
–P xyz –t "," –c
Starting copy...

504 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.): total: 10 Average: (50400.00 rows per sec.)

Here’s a sample of the data in the Products.dat file that was just exported:

1,Adjustable Race,AR-5381,0,0,,
2,Bearing Ball,BA-8327,0,0,,
3,BB Ball Bearing,BE-2349,1,0,,
4,Headset Ball Bearings,BE-2908,0,0,,
. . .

Now let’s look at importing data into SQL Server 2008.

Let’s say that each salesperson is providing a flat file that contains his or her new sales orders summaries. These files are emailed to a person in the ordering department and need to be imported into SQL Server every week. The file that you will import will be a comma-delimited file (.csv) that the salesperson created using Excel. The new entries will be the salesperson’s hottest customer’s totals. A sample input data file (named BCPHotCustomersPlus.csv) is included on the book’s CD for this chapter.

You need to specify the following with bcp in this case:

  • The full table name (in this case, AdventureWorks2008.Sales.HotCustomersPlus).

  • The direction of bcp (IN in this case because it is importing data).

  • The names of the data files that contain the import data (in this case, BCPHotCustomersPlus.csv). The following is a sample of the input data file (BCPHotCustomersPlus.csv):

    268,Cycle Parts and Accessories,S,9,Australia,954,Touring-1000 Yellow,7152.210000
    268,Cycle Parts and Accessories,S,9,Australia,955,Touring-1000 Yellow,7152.210000
    268,Cycle Parts and Accessories,S,9,Australia,956,Touring-1000 Yellow,5721.768000
    268,Cycle Parts and Accessories,S,9,Australia,968,Touring-1000 Blue,5721.768000
    . . .


  • The server name (in this case, –S DBARCH-LT2\SQL08DE01)

  • The username SA (in this case, –U sa).

  • The password (in this case, –P xyz).

  • A comma as the column delimiter (in this case, –t ",").

  • That this should be exported in character data format (in this case, –c).

  • The –q option (in this case, –q), to be sure quoted identifiers are handled properly.

At the command prompt, you execute the following:

C:> BCP AdventureWorks2008.Sales.HotCustomersPlus IN BCPHotCustomersPlus.csv
–S DBARCH-LT\SQL08DE01 –U sa –P xyz –t "," –c –q
Starting copy...

4 rows copied.
Network packet size (bytes): 4096
Clock Time (ms.): total : 241 Average : (99.59 rows per sec.)


A quick SELECT * from the HotCustomersPlus table shows the success of this operation:
CustomerID     CustomerName     CustomerType     TerritoryID     TerritoryName
268 Cycle Parts and Accessories S 9 Australia 954
268 Cycle Parts and Accessories S 9 Australia 955
268 Cycle Parts and Accessories S 9 Australia 956
268 Cycle Parts and Accessories S 9 Australia 968
. . .


The sales team can now send in their sales orders as they make sales. This brief example illustrates the beauty and power of using bcp.

The following sections look at how bcp can work with basic data representations (character, native, or Unicode), the use of a format file, and a few other extended bcp capabilities.

Other -----------------
- SQL Server Integration Services : Connection Projects in Visual Studio
- SQL Server Integration Services : The Package Execution Utility (part 3) - The dtutil Utility
- SQL Server Integration Services : The Package Execution Utility (part 2) - Running Packages
- SQL Server Integration Services : The Package Execution Utility (part 1)
- SQL Server Integration Services : The SSIS Designer
- SQL Server Integration Services : Running the SSIS Wizard
- SQL Server Integration Services : A Data Transformation Requirement
- SQL Server 2008 : SSIS Tools and Utilities
- SQL Server 2008 : SSIS Architecture and Concepts
- SQL Server 2008 : SQL Server Integration Services - SSIS Basics
- Defensive Error Handling : Using Transactions and XACT_ABORT to Handle Errors
- Managing Security Within the Database Engine : Securables
- Managing Security Within the Database Engine : Database Security
- Managing Security Within the Database Engine : Creating SQL Server Principals
- SQL Server 2008 : Performance Tuning - Locks, Blocking, and Deadlocks
- SQL Server 2008 : Performance Tuning - Tracing
- SQL Server 2008 : Implementing Error Handling - Managing and Raising User-Defined Errors
- SQL Server 2008 : Implementing Error Handling - Understanding Errors
- Implementing SQL Server Objects Using Managed Code (part 2)
- Implementing SQL Server Objects Using Managed Code (part 1)
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us